<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp"%>
<%
	String uuid = UUID.randomUUID().toString();
	request.setAttribute("uuid", uuid);
%>
<div id="${uuid}_layout" class="row am-margin-top-sm">
	<div title="业务分组" class="col-md-2">
		<div id="${uuid}_dataModelGroupTool" class="am-margin">
            <div class="btn-group">
            	<shiro:hasPermission name="DATAMODELGROUP_ADD">
                <a href="javascript:void(0)" class="btn btn-sm btn-success addBt">
                   	<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
                </a>
                </shiro:hasPermission>
                
                <shiro:hasPermission name="DATAMODELGROUP_MOD">
                <a href="javascript:void(0)" class="btn btn-sm btn-primary editBt">
                    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
                </a>
                </shiro:hasPermission>
                
                <shiro:hasPermission name="DATAMODELGROUP_DEL">
                <a href="javascript:void(0)" class="btn btn-sm btn-danger delBt">
                    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
                </a>
                </shiro:hasPermission>
            </div>
        </div>
        <div id="${uuid}_dataModelGroupTree" class="ztree">
			
        </div>
	</div>
	<div title="表定义" class="col-md-4">
			
			<div id="${uuid}_tableDefGridTB" class="form-inline" role="form">
				<shiro:hasPermission name="FRM_DEF_ADD">
				<div class="btn-group">
					<button type="button" class="btn btn-sm btn-success addBtn">
						<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
						添加
					</button>
					<button type="button" class="btn btn-sm btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
						<span class="caret"></span>
						<span class="sr-only"></span>
					</button>
					<ul class="dropdown-menu">
						<li>
							<a href="javascript:void(0)" class="addBtn">填写新增表单</a>
						</li>
						<li>
							<a href="javascript:void(0)" class="selectPhyBtn">选择已有的表</a>
						</li>
					</ul>
				</div>
			</shiro:hasPermission>
			
			<shiro:hasPermission name="FRM_DEF_MOD">
			<button class="btn btn-sm btn-primary editBtn">
				<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
				修改
			</button>
			</shiro:hasPermission>
			
			<shiro:hasPermission name="FRM_DEF_DEL">
			<div class="btn-group">
				<button type="button" class="btn btn-sm btn-danger delBtn">
					<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
					删除
				</button>
				<button type="button" class="btn  btn-sm btn-danger dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
					<span class="caret"></span>
					<span class="sr-only"></span>
				</button>
				<ul class="dropdown-menu">
					<li>
						<a href="javascript:void(0)" class="delBtn">删除定义信息及表</a>
					</li>
					<li>
						<a href="javascript:void(0)" class="delTableDefInfoBtn">只删除定义信息</a>
					</li>
					<li>
						<a href="javascript:void(0)" class="delTablePhyInfoBtn">只删除物理表</a>
					</li>
				</ul>
			</div>
			</shiro:hasPermission>

			<shiro:hasPermission name="FRM_DEF_BUILD">
			<div class="btn-group">
				<button type="button" class="btn btn-sm btn-default buildTableBtn">
					<span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
					生成表
				</button>
				<button type="button" class="btn btn-sm btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
					<span class="caret"></span>
					<span class="sr-only"></span>
				</button>
				<ul class="dropdown-menu">
					<li>
						<a href="javascript:void(0)" class="buildTableBtn">生成物理表</a>
					</li>
					<li>
						<a href="javascript:void(0)" class="rebuildTableBtn">重新生成表</a>
					</li>
				</ul>
			</div>
			</shiro:hasPermission>
			
			
			
			<button type="button" class="btn btn-sm btn-default buildFormViewBtn">
				<span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>
					生成表单
			</button>
		</div>
		<table id="${uuid}_tableDefGrid">

		</table>
	</div>
	<div class="col-md-6" id="${uuid}_formDeflayout">
		<ul class="nav nav-tabs" role="tablist">
			<li role="presentation" class="active">
				<a href="#${uuid}_fieldDefGridTab" role="tab" data-toggle="tab">
					<span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span>
					字段定义
				</a>
			</li>
			<li role="presentation">
				<a href="#${uuid}_eventDefTab" role="tab" data-toggle="tab">
					<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
					事件定义
				</a>
			</li>
		</ul>
		<div class="tab-content">
			<div role="tabpanel" class="tab-pane fade in  active" id="${uuid}_fieldDefGridTab">
				<div id="${uuid}_fieldDefGridTB" class="form-inline" role="form">
					<div class="btn-group">
						<shiro:hasPermission name="FRM_FIELD_DEF_ADD">
						<button class="btn btn-sm btn-success addBtn">
							<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
							添加
						</button>
						</shiro:hasPermission>
						
						<shiro:hasPermission name="FRM_FIELD_DEF_MOD">
						<button class="btn btn-sm btn-primary editBtn">
							<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>
							修改
						</button>
						</shiro:hasPermission>
						
						<shiro:hasPermission name="FRM_FIELD_DEF_DEL">
						<button class="btn btn-sm btn-danger delBtn">
							<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
							删除
						</button>
						</shiro:hasPermission>
					</div>
				</div>

				<table id="${uuid}_fieldDefGrid">

				</table>
			</div>
			<div role="tabpanel" class="tab-pane fade" id="${uuid}_eventDefTab">
				<div class="am-margin-top-sm" >
					<form role="form" id="${uuid}_tableOptEventDefFrom">
					<input type="hidden" name="id">
					<input type="hidden" name="tableId">
					<div class="form-group">
						<div class="btn-group">
							<shiro:hasPermission name="FRM_OPTEVENT_DEF_SAVE">
							<a href="javascript:void(0);" class="btn btn-sm btn-success addBtn">
								<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
								保存
							</a>
							</shiro:hasPermission>
							
							<shiro:hasPermission name="FRM_OPTEVENT_DEF_DEL">
							<a href="javascript:void(0);" class="btn btn-sm btn-danger delBtn">
								<span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
								注销
							</a>
							</shiro:hasPermission>
						</div>
					</div>
					<div class="form-group">
						<input type="text" class="form-control" placeholder="spring bean name" name="beanName">
					</div>
					</form>
					<h1 class="page-header">使用说明</h1>
					<ol>
						<li>
							<p>实现com.wiseMen.formDef.optEvent.OptEventInterface接口</p>
						</li>
						<li>
							<p>befoceSave方法：在保存前触发，如果返回false，则不执行保存方法</p>
						</li>
						<li>
							<p>
							befoceInsert方法：在insert方法前，在befoceSave后触发。
							前提是befoceSave返回true。
							如果befoceInsert方法返回false，则不执行insert方法。
							</p>
						</li>
						<li>
							<p>
							befoceUpdate方法：在update方法前，在befoceSave后触发。
							前提是befoceSave返回true。
							如果befoceUpdate方法返回false，则不执行update方法。
							</p>
						</li>
						<li>
							<p>
							afterSave方法：在保存成功后触发。
							</p>
						</li>
						<li>
							<p>
							afterInsert方法：在insert成功后触发。
							</p>
						</li>
						<li>
							<p>
							afterUpdate方法：在update成功后触发。
							</p>
						</li>
						<li>
							<p>
							执行顺序如下befoceSave -> befoceInsert/befoceUpdate ->afterInsert/afterUpdate ->afterSave。
							</p>
						</li>
						<li>
							<p>
							FormTableDefineEntity参数：表单定义实体，内有表单定义、字段定义列表
							</p>
						</li>
						<li>
							<p>
							FormTableData参数：用户提交表单时的数据，已对主子表关联字段进行处理
							</p>
						</li>
						<li>
							<p>
							Map&#60;String,FormFieldData&#62;&nbsp;form参数：实际需要保存到数据库中的值，其中包括一些用户、组织机构、创建时间等必要字段
							</p>
						</li>
					</ol>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript">
	$(function() {
		var uuid = '${uuid}';

		var _formDeflayout = $("#" + uuid + "_formDeflayout").tab();
		
		var _dataModelGroupTree = $("#" + uuid + "_dataModelGroupTree");


		_dataModelGroupTree = $.fn.zTree.init(_dataModelGroupTree, {
            treeId: "_dataModelGroupTree",
            async: {
                enable: true,
                url: ctx + "/dataDicAction/queryDataDicItemTreeNodes.do?dataDicCode=DATAMODEL_GROUP",
                autoParam: ["id=pid"],
                dataFilter: function (treeId, parentNode, responseData) {
                    if (responseData) {
                        $.each(responseData, function (i, n) {
                            if (n.childrenSize > 0) {
                                n.isParent = true;
                            }
                        });
                    }
                    return responseData;
                }
            },
            data: {
                key: {
                    name: "text"
                }
            },
            edit: {
            },
            callback: {
                onClick: function (e, treeId, treeNode, clickFlag) {
                    if (clickFlag != 0) {
                        var dataModelGroupCode = treeNode.code;
                        tableDefGrid.dataModelGroupCode = dataModelGroupCode;
                        tableDefGrid.bootstrapTable("refresh", {
        					query : {
        						dataModelGroupCode : dataModelGroupCode
        					}
        				});
                    }
                }
            }
        }, [{
                text: "业务分组",
                id: "-1",
                isParent: true,
                open: true
            }]);
		_dataModelGroupTree.reAsyncChildNodes(_dataModelGroupTree.getNodesByParam("id", "-1")[0], "refresh");

        var _dataModelGroupTool=$("#" + uuid + "_dataModelGroupTool");
        
        _dataModelGroupTool.find(".addBt").click(function () {
            var nodes = _dataModelGroupTree.getSelectedNodes();
            var pid = '-1';
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                pid = node.id;
            }
            var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
            	var cnode = args.obj;
            	_dataModelGroupTree.addNodes(node, cnode);
                dialog.close();
            });
            var dialog = editDataModelGroup({
                pid: pid,
                onClose: function () {
                    sub.remove();
                }
            });

        });

        _dataModelGroupTool.find(".editBt").click(function () {
            var nodes = _dataModelGroupTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
                var node = nodes[0];
                var sub = top.webUtils.topicSubscribe("dataDicItem.edit.success", function (args) {
                    var cnode = args.obj;
                    _dataModelGroupTree.updateNode($.extend(node, cnode));
                    dialog.close();
                });
                var dialog = editDataModelGroup({
                    id: node.id,
                    onClose: function () {
                        sub.remove();
                    }
                });
            } else {
            	webUtils.alert("请选择其中一个");
            }
        });

        _dataModelGroupTool.find(".delBt").click(function () {
            var nodes = _dataModelGroupTree.getSelectedNodes();
            if (nodes && nodes.length > 0) {
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var node = nodes[0];
		                var ids = [];
		                ids.push(node.id);
		                $.post(ctx + "/dataDicAction/delDataDicItems.do", {ids: ids.join(",")}, function (data) {
		                    if (data.success) {
		                    	_dataModelGroupTree.removeNode(node);
		                    } else {
		                        webUtils.error(data.msg);
		                    }
		                }, 'json');
					}
        		});

            } else {
            	webUtils.alert("请选择需要删除的");
            }
        });

        var editDataModelGroup = function (params) {
            params = params || {};
            var t = [];
            if (params.id) {
                t.push("id=" + params.id);
            }
            if (params.pid) {
                t.push("pid=" + params.pid);
            }
            t.push("dataDicCode=DATAMODEL_GROUP");
            
            var dialog=webUtils.dialog({
           	 title:'编辑系统',
           	 url:ctx + '/dataDicAction/dataDicItemEditView.do?' + t.join("&"),
           	 onremove:function () {
                    if (params.onClose) {
                        params.onClose.call(null);
                    }
                }
            });
            return dialog;
        };
        
        
		var tableDefGrid = $("#" + uuid + "_tableDefGrid").bootstrapTable({
			url : ctx + "/formTableDefineAction/queryTableDef.do",
			columns : [ {
				field : '_id',
				checkbox : true
			}, {
				title : '表编码',
				field : 'tableCode',
				align : 'left',
				width : 120
			}, {
				title : '表英文名',
				field : 'tableName',
				align : 'left',
				width : 120
			}, {
				title : '表描述',
				field : 'tableDesc',
				align : 'left',
				width : 120
			}, {
				title : '主表',
				field : 'isMain',
				align : 'left',
				width : 40,
				formatter : function(value, row, index) {
					if (value == 1) {
						return "是";
					} else {
						return "";
					}
				}
			}, {
				title : '生成',
				field : 'hasBuilt',
				align : 'left',
				width : 40,
				formatter : function(value, row, index) {
					if (value == 1) {
						return "是";
					} else {
						return "";
					}
				}
			} ],
			singleSelect : true,
			toolbar : "#" + uuid + "_tableDefGridTB",
			onClickRow : function(row, elm) {
				fieldDefGrid.tableId = row.id;
				fieldDefGrid.bootstrapTable("refresh", {
					query : {
						tableId : row.id
					}
				});
				loadOptEvent(row.id);
			},
        	queryParams:function(params){
        		if(tableDefGrid){
        			params.dataModelGroupCode=tableDefGrid.dataModelGroupCode;
        		}
        		return params;
        	}
		});

		var _tableDefGridTB = $("#" + uuid + "_tableDefGridTB");
		_tableDefGridTB.find(".addBtn").click(
				function() {
					if(tableDefGrid.dataModelGroupCode){
						var sub = top.webUtils.topicSubscribe(
								"tableDefine.edit.success", function(args) {
									dialog.close();
									tableDefGrid.bootstrapTable("refresh");
								});
						var dialog = editTableDefine({
							dataModelGroupCode:tableDefGrid.dataModelGroupCode,
							onClose : function() {
								sub.remove();
							}
						});
					}else{
						webUtils.error("请选择具体的业务分组");
					}
				});
		
		
		_tableDefGridTB.find(".selectPhyBtn").click(
				function() {
					alert("暂时未提供该功能。");
				});
		
		

		_tableDefGridTB.find(".editBtn").click(
				function() {
					var rows = tableDefGrid.bootstrapTable("getSelections");
					if (rows && rows.length > 0) {
						var sub = top.webUtils.topicSubscribe(
								"tableDefine.edit.success", function(args) {
									dialog.close();
									tableDefGrid.bootstrapTable("refresh");
								});
						var row = rows[0];
						var dialog = editTableDefine({
							id : row.id,
							onClose : function() {
								sub.remove();
							}
						});
					} else {
						webUtils.alert("请选择需要编辑的数据");
					}
				});

		_tableDefGridTB.find(".delBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
						$.each(rows, function(i, n) {
							selecteds.push(n.id);
						});
						$.post(ctx + "/formTableDefineAction/delTableDefs.do", {
							ids : selecteds.join(",")
						}, function(data) {
							if (data.success) {
								webUtils.alert("操作成功");
								tableDefGrid.bootstrapTable("refresh");
							} else {
								webUtils.alert(data.msg);
							}
						}, 'json');
					}
				})
				
			} else {
				webUtils.alert("请选择需要删除的数据");
			}
		});
		
		_tableDefGridTB.find(".delTableDefInfoBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
						$.each(rows, function(i, n) {
							selecteds.push(n.id);
						});
						$.post(ctx + "/formTableDefineAction/delTableDefInfos.do", {
							ids : selecteds.join(",")
						}, function(data) {
							if (data.success) {
								webUtils.alert("操作成功");
								tableDefGrid.bootstrapTable("refresh");
							} else {
								webUtils.alert(data.msg);
							}
						}, 'json');
					}
				});
				
			} else {
				webUtils.alert("请选择需要删除的数据");
			}
		});
		
		_tableDefGridTB.find(".delTablePhyInfoBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
						$.each(rows, function(i, n) {
							selecteds.push(n.id);
						});
						$.post(ctx + "/formTableDefineAction/delTablePhyInfos.do", {
							ids : selecteds.join(",")
						}, function(data) {
							if (data.success) {
								webUtils.alert("操作成功");
								tableDefGrid.bootstrapTable("refresh");
							} else {
								webUtils.alert(data.msg);
							}
						}, 'json');
					}
				});
				
			} else {
				webUtils.alert("请选择需要删除的数据");
			}
		});
		
		_tableDefGridTB.find(".buildTableBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				var row = rows[0];
				if (row.hasBuilt == 1) {
					webUtils.alert("该表已生成，不可重复该操作");
				} else {
					$.post(ctx + "/formTableDefineAction/buildFormTable.do", {
						id : row.id
					}, function(data) {
						if (data.success) {
							tableDefGrid.bootstrapTable("refresh");
							webUtils.alert("成功生成库表！");
						} else {
							webUtils.error(data.msg);
						}
					}, 'json');
				}

			} else {
				webUtils.alert("请选择需要生成库表的数据");
			}
		});
		
		_tableDefGridTB.find(".rebuildTableBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				webUtils.confirm("请确认是否重建表结构。该操作将删除所有数据，请备份数据后再执行。",function(result){
					if(result){
						var row = rows[0];
						$.post(ctx + "/formTableDefineAction/rebuildFormTable.do", {
							id : row.id
						}, function(data) {
							if (data.success) {
								tableDefGrid.bootstrapTable("refresh");
								webUtils.alert("成功生成库表！");
							} else {
								webUtils.error(data.msg);
							}
						}, 'json');
					}
				});
			}else{
				webUtils.alert("请选择需要生成库表的数据");
			}
		});
		
		_tableDefGridTB.find(".buildFormViewBtn").click(function() {
			var rows = tableDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				var row = rows[0];
				webUtils.dialog({
					title:"快速生成表单",
					url:ctx+"/formBuilderAct/fastView?id="+row.id
				});
			}else{
				webUtils.alert("请选择需要生成表单视图的数据");
			}
			
		});
		
		_tableDefGridTB.find(".expBtn").click(function(){
        	var rows=tableDefGrid.bootstrapTable("getSelections");
        	if(rows && rows.length>0){
        		var ids = [];
                $.each(rows, function (i, n) {
                	ids.push(n.id);
                });
        		window.open(ctx + "/dataModelDefAct/export.do?ids="+ids.join(","));
        	} else {
        		webUtils.alert("请选择需要导出的数据");
            }
        });
        
        var dataModelUploader
        webUtils.fileUploader({
        	server:ctx+"/dataModelDefAct/import.do",
        	uploadPick:"#impDataModelDefBtn",
        	duplicate:false,
        	accept: {
                title: 'json文件类型',
                extensions: 'json'
            },
            afterInit:function(u,uder){
            	dataModelUploader=u;
            }
        });
        
        _tableDefGridTB.find(".impBtn").mouseenter(function(){
        	dataModelUploader.refresh();
        });
		

		var editTableDefine = function(params) {
			params = params || {};
			var p = [];
			if (params.id) {
				p.push("id=" + params.id);
			}
			if(params.dataModelGroupCode){
				p.push("dataModelGroupCode="+params.dataModelGroupCode);
			}

			var dialog = BootstrapDialog.show({
				title : '编辑表定义',
				url : ctx + '/formTableDefineAction/editView.do?'+ p.join("&"),
				onhidden : function() {
					if (params.onClose) {
						params.onClose.call(null);
					}
				}
			});
			return dialog;
		}

		var fieldDefGrid = $("#" + uuid + "_fieldDefGrid")
				.bootstrapTable(
						{
							url : ctx
									+ "/formFieldDefineAction/queryFieldDef.do",
							columns : [
									{
										field : '_id',
										checkbox : true
									},
									{
										title : '字段',
										field : 'originalName',
										align : 'left',
										width : 120
									},
									{
										title : '描述',
										field : 'fieldName',
										align : 'left',
										width : 200
									},
									{
										title : '类型',
										field : 'fieldType',
										align : 'left',
										width : 100,
										formatter : function(value, row, index) {
											var dicItem = webUtils
													.getDataDicItemVal(
															"TABLE_DEF_FIELD_TYPE",
															value);
											if (dicItem) {
												return dicItem.name;
											}
										}
									},
									{
										title : '长度',
										field : 'charLen',
										align : 'left',
										width : 60,
										formatter : function(value, row, index) {
											if (row.fieldType == "varchar") {
												return row.charLen;
											} else if (row.fieldType == "number") {
												return row.intLen;
											} else {
												return 0;
											}
										}
									},
									{
										title : '小数点',
										field : 'decimalLen',
										align : 'left',
										width : 60
									},
									{
										title : '允许空值',
										field : 'isRequired',
										align : 'left',
										width : 60,
										formatter : function(value, row, index) {
											return value == 1 ? "" : "√";
										}
									},
									{
										title : '是否隐藏',
										field : 'isHidden',
										align : 'left',
										width : 60,
										formatter : function(value, row, index) {
											return value == 1 ? "√" : "";
										}
									},
									{
										title : '顺序',
										field : 'sn',
										align : 'left',
										width : 80,
										formatter : function(value, row, index) {
											return '<a href="javascript:void(0)" onClick="setFieldSn(this,\''
													+ row.id
													+ '\','
													+ value
													+ ')">' + value + '</a>'
										}
									} ],
							toolbar : "#" + uuid + "_fieldDefGridTB",
							queryParams : function(params) {
								if (fieldDefGrid) {
									params.tableId = fieldDefGrid.tableId;
								}
								return params;
							},
							fixedColumns: true,
				            fixedNumber:3
						});

		var _fieldDefGridTB = $("#" + uuid + "_fieldDefGridTB");
		_fieldDefGridTB.find(".addBtn").click(
				function() {
					if (fieldDefGrid.tableId) {
						var sub = top.webUtils.topicSubscribe(
								"fieldDefine.edit.success", function(args) {
									dialog.close();
									fieldDefGrid.bootstrapTable("refresh");
								});
						var dialog = editFieldDefine({
							tableId : fieldDefGrid.tableId,
							onClose : function() {
								sub.remove();
							}
						});
					} else {
						webUtils.alert("请选择具体的表定义");
					}
				});
		_fieldDefGridTB.find(".editBtn").click(
				function() {
					var rows = fieldDefGrid.bootstrapTable("getSelections");
					if (rows && rows.length > 0) {
						var sub = top.webUtils.topicSubscribe(
								"fieldDefine.edit.success", function(args) {
									dialog.close();
									fieldDefGrid.bootstrapTable("refresh");
								});
						var row = rows[0];
						var dialog = editFieldDefine({
							id : row.id,
							onClose : function() {
								sub.remove();
							}
						});
					} else {
						webUtils.alert("请选择需要编辑的数据");
					}
				});
		_fieldDefGridTB.find(".delBtn").click(function() {
			var rows = fieldDefGrid.bootstrapTable("getSelections");
			if (rows && rows.length > 0) {
				webUtils.confirm("删除后将不可恢复，请问是否继续?", function(result) {
					if(result){
						var selecteds = [];
						$.each(rows, function(i, n) {
							selecteds.push(n.id);
						});
						$.post(ctx + "/formFieldDefineAction/delFieldDefs.do", {
							ids : selecteds.join(",")
						}, function(data) {
							if (data.success) {
								fieldDefGrid.bootstrapTable("refresh");
							} else {
								webUtils.alert(data.msg);
							}
						}, 'json');
					}
				});
				
			} else {
				webUtils.alert("请选择需要删除的数据");
			}
		});

		var editFieldDefine = function(params) {
			params = params || {};
			var p = [];
			if (params.id) {
				p.push("id=" + params.id);
			}
			if (params.tableId) {
				p.push("tableId=" + params.tableId);
			}

			var dialog = BootstrapDialog.show({
				title : '编辑字段定义',
				url : ctx + '/formFieldDefineAction/editView.do?'+ p.join("&"),
				onhidden : function() {
					if (params.onClose) {
						params.onClose.call(null);
					}
				}
			});
			return dialog;
		}
		
		var _tableOptEventDefFrom=$("#"+uuid+"_tableOptEventDefFrom");
		_tableOptEventDefFrom.find(".addBtn").click(function(){
			var beanName=_tableOptEventDefFrom.find("input[name=beanName]").val();
			if(beanName){
				_tableOptEventDefFrom.ajaxSubmit({
					url : ctx + '/formDataAction/save.do',
					data : {},
					type : "POST",
					success : function(data) {
						if (data.success) {
							webUtils.alert("提交成功");
						} else {
							webUtils.error(data.msg);
						}
					}
				});
			}else{
				webUtils.error("请输入bean名称");
			}
		});
		_tableOptEventDefFrom.find(".delBtn").click(function(){
			var beanName=_tableOptEventDefFrom.find("input[name=beanName]");
			var tableId=_tableOptEventDefFrom.find("input[name=tableId]");
			$.post(ctx + "/formDataAction/delByTableId.do", {
				tableId : tableId.val()
			}, function(data) {
				if (data.success) {
					beanName.val("");
				} else {
					webUtils.alert(data.msg);
				}
			}, 'json');
		});
		
		var loadOptEvent=function(tableId){
			var data={
					id:"",
					tableId:tableId,
					beanName:""
				};
			webUtils.fullForm({
				form : _tableOptEventDefFrom,
				data : data
			});
			$.getJSON(ctx + '/formDataAction/findByTableId.do',{tableId : tableId},function(data){
				webUtils.fullForm({
					form : _tableOptEventDefFrom,
					data : data
				});
			});
		}
	});
	var setFieldSn = function(bt, id) {
		var _$bt = $(bt);
		var indexValue = _$bt.html();
		var v = prompt("请输入顺序号", indexValue);
		if (v != null) {
			$.post(ctx + "/formFieldDefineAction/updateSn.do", {
				id : id,
				sn : v
			}, function(data) {
				if (data.success) {
					_$bt.html(v);
				}
			});
		}
	}
</script>